//
//  MainViewController.m
//  VidoarDemo
//
//  Created by xm on 2025/6/17.
//

#import "KeepLiveViewController.h"
#import "Plan1ViewController.h"

@interface KeepLiveViewController ()

@property(nonatomic, strong) UIButton *button1;
@property(nonatomic, strong) UIButton *button2;
@property(nonatomic, strong) UIButton *button3;
@property (nonatomic,strong) UILabel *timeLabel;
@property (nonatomic,strong) UILabel *lastDurationLabel;
@property (nonatomic,strong) UILabel *durationLabel;

@end

@implementation KeepLiveViewController


static NSString* Color_Normal = @"5974A4";
static NSString* Color_Highlight = @"FF9700";

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.log(@"Main");
    [self createdUI];
//
    
    [m3_keeplive run];
    [self  watch];
}

-(void) test
{
    //    self.log(@"[1] %@", m3_userDefaults.planLog);
    //    NSDictionary* data = data = @{
    //        @"task" : @{
    //            @"duration": @(10),
    //            @"lastDuration": @(20),
    //        }
    //    };
    //    m3_userDefaults.planLog = data;
    //
    //    self.log(@"[2] %@", m3_userDefaults.planLog);
    //
        
}


-(void) createdUI
{
    [self.view addSubview:self.button3];
    [self.view addSubview:self.button1];
//    [self.view addSubview:self.button2];
    
    [self.view addSubview:self.durationLabel];
    [self.view addSubview:self.lastDurationLabel];
    
    
    
}



-(void) watch
{
    [[self.button1 rac_signalForControlEvents:UIControlEventTouchDown] subscribeNext:^(__kindof UIControl * _Nullable x) {
        self.log(@"切换Task模式");
        if(m3_keeplive.mode !=KeepLiveService_Mode_Task){
            [m3_keeplive stop];
            [m3_keeplive switchMode:KeepLiveService_Mode_Task];
        }
        
    }];
    
    [[self.button2 rac_signalForControlEvents:UIControlEventTouchDown] subscribeNext:^(__kindof UIControl * _Nullable x) {
        self.log(@"切换Audio模式");
        if(m3_keeplive.mode !=KeepLiveService_Mode_Audio){
            [m3_keeplive stop];
            [m3_keeplive switchMode:KeepLiveService_Mode_Audio];
        }
    }];
    
    [[self.button3 rac_signalForControlEvents:UIControlEventTouchDown] subscribeNext:^(__kindof UIControl * _Nullable x) {
        self.log(@"切换Location模式");
        if(m3_keeplive.mode !=KeepLiveService_Mode_Location){
            [m3_keeplive stop];
            [m3_keeplive switchMode:KeepLiveService_Mode_Location];
        }
    }];
    
    [RACObserve(m3_keeplive, duration) subscribeNext:^(id  _Nullable x) {
        NSString* str = [self formatDuration:m3_keeplive.duration];
        self.durationLabel.text = str;
    }];
    [RACObserve(m3_keeplive, lastDuration) subscribeNext:^(id  _Nullable x) {
        NSString* str = [self formatDuration:m3_keeplive.lastDuration];
        self.lastDurationLabel.text = [NSString stringWithFormat:@"上次：%@", str];
    }];
    [RACObserve(m3_keeplive, mode) subscribeNext:^(id  _Nullable x) {
        if(m3_keeplive.mode ==KeepLiveService_Mode_Task){
            [self.button1 setBackgroundColor:[UIColor colorWithHexString:Color_Highlight]];
        }else{
            [self.button1 setBackgroundColor:[UIColor colorWithHexString:Color_Normal]];
        }
        
        if(m3_keeplive.mode ==KeepLiveService_Mode_Audio){
            [self.button2 setBackgroundColor:[UIColor colorWithHexString:Color_Highlight]];
        }else{
            [self.button2 setBackgroundColor:[UIColor colorWithHexString:Color_Normal]];
        }
        
        if(m3_keeplive.mode ==KeepLiveService_Mode_Location){
            [self.button3 setBackgroundColor:[UIColor colorWithHexString:Color_Highlight]];
        }else{
            [self.button3 setBackgroundColor:[UIColor colorWithHexString:Color_Normal]];
        }
    }];


}



- (UIButton *)button1
{
    if(_button1){
        return _button1;
    }
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"后台任务模式" forState:UIControlStateNormal];
    button.top = NavAndStatusToolHeight + 30;
    button.left = 20;
    button.width = screenWidth - 40;
    button.height = 48;
    
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button setTitleColor:[UIColor colorWithHexString:@"FFFFFF"] forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont systemFontOfSize:20]];
    [button setBackgroundColor:[UIColor colorWithHexString:@"5974A4"]];
    
    _button1 = button;
    return _button1;
}


- (UIButton *)button2
{
    if(_button2){
        return _button2;
    }
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"音频模式" forState:UIControlStateNormal];
    button.top = self.button1.bottom + 20;
    button.left = 20;
    button.width = screenWidth - 40;
    button.height = 48;
    
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button setTitleColor:[UIColor colorWithHexString:@"FFFFFF"] forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont systemFontOfSize:20]];
    [button setBackgroundColor:[UIColor colorWithHexString:@"5974A4"]];
    
    _button2 = button;
    return _button2;
}


- (UIButton *)button3
{
    if(_button3){
        return _button3;
    }
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"定位模式" forState:UIControlStateNormal];
    button.top = self.button1.bottom + 20;
    button.left = 20;
    button.width = screenWidth - 40;
    button.height = 48;
    
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button setTitleColor:[UIColor colorWithHexString:@"FFFFFF"] forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont systemFontOfSize:20]];
    [button setBackgroundColor:[UIColor colorWithHexString:@"5974A4"]];
    
    _button3 = button;
    return _button3;
}





- (UILabel *)durationLabel
{
    if(_durationLabel != nil){
        return _durationLabel;
    }
    
    UILabel* lb = [[UILabel alloc] init];
    lb.top = screenHeight/2 - 100;
    lb.left = 20;
    lb.width = screenWidth - 40;
    lb.height = 50;
    lb.text = @"";
    lb.font = [UIFont systemFontOfSize:50];
    lb.textColor = [UIColor colorWithHexString:@"FF9700"];
    lb.textAlignment = NSTextAlignmentCenter;
    
    _durationLabel = lb;
    
    return _durationLabel;
}


- (UILabel *)lastDurationLabel
{
    if(_lastDurationLabel != nil){
        return _lastDurationLabel;
    }
    
    UILabel* lb = [[UILabel alloc] init];
    lb.top = screenHeight - 200;
    lb.left = 20;
    lb.width = screenWidth - 40;
    lb.height = 50;
    lb.text = @"";
    lb.font = [UIFont systemFontOfSize:50];
    lb.textColor = [UIColor colorWithHexString:@"00E16F"];
    lb.textAlignment = NSTextAlignmentCenter;
    
    _lastDurationLabel = lb;
    
    return _lastDurationLabel;
}


-(NSString*) formatDuration:(NSInteger)seconds
{
    NSInteger second = seconds % 60;
    seconds -= second;
    
    NSInteger minute = round((seconds % 3600)/60);
    seconds -= minute*60;
    
    NSInteger hour = round(seconds / 3600);
    
    
    return  [NSString stringWithFormat:@"%02ld:%02ld:%02ld", hour, minute, second];
    
    
}

@end
